package domain.cms;

import domain.AbstractEntity;
import domain.File;
import domain.User;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.IndexedEmbedded;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Abstract class for CmsPage, CmsCategory and CmsSection.
 */
@MappedSuperclass
public abstract class AbstractPage extends AbstractEntity {

    @Cascade(CascadeType.ALL)
    @OneToOne
    @IndexedEmbedded
    protected CmsAttribute attribute = new CmsAttribute();
    @OneToMany(fetch = FetchType.LAZY)
    protected List<File> files = new ArrayList<File>();
    @OneToOne(fetch = FetchType.LAZY)
    protected File avatar;
    @OneToOne(fetch = FetchType.LAZY)
    @IndexedEmbedded
    protected User user;
    @Field
    @Temporal(javax.persistence.TemporalType.DATE)
    protected Date startPublishing;
    @Field
    @Temporal(javax.persistence.TemporalType.DATE)
    protected Date finishPublishing;
    @Column(name = "ORDER_POSITION")
    @IndexedEmbedded
    protected Integer order;

    public static final String TITLE = "title";
    public static final String LINK_NAME = "linkName";
    public static final String DESCRIPTION = "description";
    public static final String KEYWORDS = "keywords";
    public static final String CONTENT = "content";
    public static final String URL = "url";
    public static final String PUBLISHED = "published";

    public void removeOldCmsUrl(String url) throws Exception {
        attribute.removeOldCmsUrl(url);
    }

    public CmsUrl addOldCmsUrl(String url) {
        return this.attribute.addOldCmsUrl(url);
    }

    public String getLinkName() {
        if (attribute.getLinkName() == null || attribute.getLinkName().equals("")) {
            return getTitle();
        }

        return attribute.getLinkName();
    }

    public void setLinkName(String linkName) {
        attribute.setLinkName(linkName);
    }

    public CmsUrl getUrlAsCmsUrl() {

        return attribute.getUrlAsCmsUrl();
    }

    public String getUrl() {

        return attribute.getUrl();
    }

    public void setUrl(String url) {
        attribute.setUrl(url.replaceAll(" ", "-"));
    }

    public List<CmsUrl> getOldUrls() {
        return attribute.getOldUrls();
    }

    @Override
    public String toString() {
        return getTitle();
    }

    protected AbstractPage() {
    }

    public CmsAttribute getAttribute() {
        return attribute;
    }

    public void setAttribute(CmsAttribute attribute) {
        this.attribute = attribute;
    }

    public List<File> getFiles() {
        return files;
    }

    public void setFiles(List<File> files) {
        this.files = files;
    }

    public File getAvatar() {
        return avatar;
    }

    public void setAvatar(File avatar) {
        this.avatar = avatar;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Date getStartPublishing() {
        return startPublishing;
    }

    public void setStartPublishing(Date startPublishing) {
        this.startPublishing = startPublishing;
    }

    public Date getFinishPublishing() {
        return finishPublishing;
    }

    public void setFinishPublishing(Date finishPublishing) {
        this.finishPublishing = finishPublishing;
    }

    public Integer getOrder() {
        return order;
    }

    public void setOrder(Integer order) {
        this.order = order;
    }

    public int compareTo(CmsAttribute o) {
        return attribute.compareTo(o);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof AbstractPage)) return false;
        if (!super.equals(o)) return false;

        AbstractPage page = (AbstractPage) o;

        if (attribute != null ? !attribute.equals(page.attribute) : page.attribute != null) return false;
        if (finishPublishing != null ? !finishPublishing.equals(page.finishPublishing) : page.finishPublishing != null)
            return false;
        if (order != null ? !order.equals(page.order) : page.order != null) return false;
        if (startPublishing != null ? !startPublishing.equals(page.startPublishing) : page.startPublishing != null)
            return false;

        return true;
    }

    public String getContent() {
        return attribute.getContent();
    }

    public String getDescription() {
        return attribute.getDescription();
    }

    public String getKeywords() {
        return attribute.getKeywords();
    }

    public Date getLastEditedDate() {
        return attribute.getLastEditedDate();
    }

    public Integer getOrderPosition() {
        return attribute.getOrderPosition();
    }

    public String getTitle() {
        return attribute.getTitle();
    }

    @Override
    public int hashCode() {
        return attribute.hashCode();
    }

    public boolean isPublished() {
        return attribute.isPublished();
    }

    public void setContent(String content) {
        attribute.setContent(content);
    }

    public void setDescription(String description) {
        attribute.setDescription(description);
    }

    public void setKeywords(String keywords) {
        attribute.setKeywords(keywords);
    }

    public void setOrderPosition(Integer orderPosition) {
        attribute.setOrderPosition(orderPosition);
    }

    public void setPublished(boolean published) {
        attribute.setPublished(published);
    }

    public void setTitle(String title) {
        attribute.setTitle(title);
    }
}
